<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addEmployee" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <!-- 遍历只能遍历组件的数据 -->
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" />
        <!-- 放置一个tree组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :default-expand-all="true"
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 得到员工列表的接口
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
// 列表转树形
import { transListToTreeData } from '@/utils'
// 引入员工映射关系 格式化聘用关系
import EmployeeEnum from '@/constant/employees'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum, // 在data中定义数据
      treeData: [], // 部门树形数据
      showTree: false, // 展示隐藏部门树形数据
      loading: false, // 加载状态
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        correctionTime: ''// 转正时间
      },
      formRules: {
        username:
         [
           { required: true, message: '用户姓名不能为空', trigger: 'blur' },
           { min: 1, max: 8, message: '用户姓名为1-4位', trigger: 'blur' }
         ],
        mobile:
        [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment:
         [
           { required: true, message: '聘用形式不能为空', trigger: 'blur' }
         ],
        workNumber:
        [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName:
        [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry:
        [
          { required: true, message: '入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   聚焦表单中部门输入框时触发
    async getDepartments() {
      //   显示树形
      this.showTree = true
      //   开启加载
      this.loading = true
      //   得到部门列表
      const { depts } = await getDepartments()
      //   转换成树形
      this.treeData = transListToTreeData(depts, '')
      //   关闭加载
      this.loading = false
    },
    // 点击树形中某个部门时触发
    selectNode(node) {
      // 更新选择部门
      this.formData.departmentName = node.name
      //   隐藏树形
      this.showTree = false
    },
    // 点击确定执行
    async btnOk() {
      try {
        // 手动校验表单
        await this.$refs.addEmployee.validate()
        // 调新增接口
        await addEmployee(this.formData)
        // 更新视图
        this.$parent.getEmployeeList()
        // 关闭弹窗 触发了close绑定的btnCancel 清空了 不用再清空了
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消执行
    btnCancel() {
      // 重置数据
      this.formData = {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        correctionTime: ''// 转正时间
      }
      // 重置校验结果
      this.$refs.addEmployee.resetFields()
      //   关闭弹层 配合.sync
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
